local skel = fk.CreateSkill {
  name = "emo__liaosi",
  tags = {Skill.Limited}
}

Fk:loadTranslationTable{
  ["emo__liaosi"] = "疗丝",
  [":emo__liaosi"] = "限定技，当一名角色进入濒死状态时，你可以移去所有“魔物”并视为对该角色使用等量张【树上开花】，然后令其回复X点体力（X为其以此法弃置牌的花色数）。你死亡后，该角色每回合开始时失去一点体力。",

  ["#emo__liaosi-invoke"] = "疗丝：你可以移去所有“魔物”并视为对 %dest 使用等量张【树上开花】",
  ["@emo__liaosi-phase"] = "疗丝",
  ["@@emo__liaosi_losehp"] = "疗丝:负面",

  ["$emo__liaosi1"] = "",
  ["$emo__liaosi2"] = "",
}

skel:addEffect(fk.EnterDying, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target.dying and not target.dead and not target:isNude()
    and #player:getPile("yinwu_monster") > 0 and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
    and not player:prohibitUse(Fk:cloneCard("bogus_flower")) and not player:isProhibited(target, Fk:cloneCard("bogus_flower"))
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#emo__liaosi-invoke::"..target.id })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addTableMark(player, "emo__liaosi_target", target.id)
    local cards = player:getPile("yinwu_monster")
    room:moveCards({
      from = player,
      ids = cards,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonPutIntoDiscardPile,
      proposer = player,
      skillName = skel.name,
    })
    for _ = 1, #cards do
      if target.dead then break end
      local card = Fk:cloneCard("bogus_flower")
      card.skillName = skel.name
      if player:prohibitUse(Fk:cloneCard("bogus_flower")) or player:isProhibited(target, Fk:cloneCard("bogus_flower")) then break end
      room:useCard({
        from = player,
        card = card,
        tos = {target},
        extra_data = {emo__liaosi_target = target.id},
      })
      room:delay(200)
    end
    if target.dead then return end
    local num = #target:getTableMark("@emo__liaosi-phase")
    room:setPlayerMark(target, "@emo__liaosi-phase", 0)
    if num > 0 and target:isAlive() then
      room:recover({ who = target,  num = num, recoverBy = player, skillName = skel.name })
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player.dead then return false end
    local e = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
    if e then
      local use = e.data
      if use.card.name == "bogus_flower" and use.extra_data and use.extra_data.emo__liaosi_target == player.id then
        for _, move in ipairs(data) do
          if move.moveReason == fk.ReasonDiscard and move.from == player then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local mark = player:getTableMark("@emo__liaosi-phase")
    for _, move in ipairs(data) do
      if move.moveReason == fk.ReasonDiscard and move.from == player then
        for _, info in ipairs(move.moveInfo) do
          table.insertIfNeed(mark, Fk:getCardById(info.cardId):getSuitString(true))
        end
      end
    end
    player.room:setPlayerMark(player, "@emo__liaosi-phase", mark)
  end,
})

skel:addEffect(fk.TurnStart, {
  is_delay_effect = true,
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@@emo__liaosi_losehp") > 0 and target == player and not player.dead
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:loseHp(player, 1, skel.name)
  end,
})

skel:addEffect(fk.Death, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("emo__liaosi_target") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    for _, pid in ipairs(player:getMark("emo__liaosi_target")) do
      player.room:setPlayerMark(player.room:getPlayerById(pid), "@@emo__liaosi_losehp", 1)
    end
  end,
})

return skel
